package cn.doctor.assistant.pad.ui.hudong;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.lidroid.xutils.BitmapUtils;

import java.util.List;

import cn.doctor.assistant.pad.R;
import cn.doctor.assistant.pad.entity.ContactInfo;
import cn.doctor.assistant.pad.entity.GroupsBean;

/**
 * Created by jiaolf on 2016/4/26.
 */
public class GroupListAdapter extends BaseExpandableListAdapter {
    private LayoutInflater mInflater;
    private BitmapUtils bmpUtils;
    private List<GroupsBean> groups;

    public GroupListAdapter(Context context, List<GroupsBean> groupsList) {
        bmpUtils = new BitmapUtils(context);
        bmpUtils.configDefaultLoadingImage(R.drawable.default_avatar);
        bmpUtils.configDefaultLoadFailedImage(R.drawable.default_avatar);
        mInflater = LayoutInflater.from(context);
        this.groups = groupsList;
    }

    @Override
    public int getGroupCount() {
        return groups.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return groups.get(groupPosition).getUsers() == null ? 0 : groups.get(groupPosition).getUsers().size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return groups.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return groups.get(groupPosition).getUsers().get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.list_chat_group_item, null);
        }

        GroupsBean group = groups.get(groupPosition);

        TextView groupName = (TextView) convertView.findViewById(R.id.group_name);
        groupName.setText(group.getGroup().getGroupname());

        //分组中，好友的数量
        TextView group_friend_count = (TextView) convertView.findViewById(R.id.group_friend_count);
        if (group.getUsers() != null) {
            group_friend_count.setText(String.format("(%d)", group.getUsers().size()));
        }else {
            group_friend_count.setText("(0)");
        }

        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.list_chat_contact_item, null);
        }

        ContactInfo contactInfo = (ContactInfo) getChild(groupPosition, childPosition);

        TextView userName = (TextView) convertView.findViewById(R.id.user_name);
        TextView userInfo = (TextView) convertView.findViewById(R.id.user_info);
        ImageView icon = (ImageView) convertView.findViewById(R.id.icon);

        userName.setText(contactInfo.getName());

        if(contactInfo.getAddress() != null) {
            userInfo.setText(contactInfo.getAddress());
        }

        if (contactInfo.getPicture() != null) {
            bmpUtils.display(icon, contactInfo.getPicture());
        }

        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }
}
